#!/bin/bash

####### definition params ######
month=201803
db=dbyb
path=/app/custom/data/workspace/shell

hdfs dfs -mkdir -p /dbyb/out/${month}

#hive -e "CREATE DATABASE IF NOT EXISTS ${db}"

####### load data ######
echo '~~ HIVE : ALL DATA START LOADING ~~'
hive -f load_data.sql --hivevar month=${month}

####### execute function ######
echo '~~ HIVE : FUNCTIONS IS RUNNING ~~'
hive -f function.sql --hivevar month=${month}
echo '*** HIVE : FUNCTIONS IS END ***'

exitCode=$?
if [ ${exitCode} -ne 0 ];then
         echo "[ERROR] hive execute failed!"
         exit ${exitCode}
fi

####### execute mr ######
input1=/dbyb/out/${month}/t_db_intermediate_lastdata
input2=''
output=/dbyb/out/${month}/t_db_calculation_results

echo '~~ HIVE : EXECUTE MAP REDUCE CALCULATION JOB STEP-1 ~~'
hive -e "use dbyb; insert overwrite directory '${input1}' row format delimited fields terminated by '|' select * from t_db_intermediate_lastdata;"

echo '~~ HIVE : EXECUTE MAP REDUCE CALCULATION JOB STEP-2 ~~'
hadoop jar dbyb_calculation.jar cn.com.sinosoft.dbyb.mr.DbybMrApplication ${input1} ${output}

exitCode2=$?
if [ ${exitCode2} -ne 0 ];then
         echo "[ERROR] hive execute failed!"
         exit ${exitCode2}
fi

####### LOAD DATA TO calculation_results ######
echo '~~ HIVE : START LOAD DATA TO RESULT TABLE ~~'
hive -e "use dbyb; LOAD DATA inpath '${output}' INTO TABLE t_db_calculation_results_2018 PARTITION ( MONTH ='${month}');"

####### OVER MESSAGE ######
echo '*** HIVE : IT IS OVER , YOU CAN SELECT DATA FROM TABLE t_db_calculation_results_2018 ***'